package cn.com.api.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiDingDan;
import cn.com.api.entity.ApiShop;
import cn.com.api.entity.ApiTOrderMessage;
import cn.com.api.entity.ApiT_message;
import cn.com.api.entity.ApiT_shipping;
import cn.com.api.entity.ApiTmember;
import cn.com.api.mapper.ApiDingDanMapper;
import cn.com.api.mapper.ApiShopsMapper;
import cn.com.api.mapper.ApiTOrderMessageMapper;
import cn.com.api.mapper.ApiT_messageMapper;
import cn.com.api.mapper.ApiT_shippingMapper;
import cn.com.api.mapper.ApiTmemberMapper;
import cn.com.api.requestpage.OrderMessagePage;
import cn.com.api.service.ApiCommonService;
import cn.com.api.service.ApiTOrderMessageService;
import cn.com.util.*;
import cn.com.util.*;

@Service
public class ApiTOrderMessageServiceImpl implements ApiTOrderMessageService {
	@Autowired
	private ApiTOrderMessageMapper apiTOrderMessageMapper;
	@Autowired
	private ApiCommonService apiCommonService;
	@Autowired
	private ApiShopsMapper apiShopsMapper;
	@Autowired
	private ApiTmemberMapper apiTmemberMapper;
	@Autowired
	private ApiT_shippingMapper apiT_shippingMapper;
	@Autowired
	private ApiDingDanMapper apiDingDanMapper;
	@Autowired
	private ApiT_messageMapper apiT_messageMapper;

	private static Logger logger = Logger.getLogger(ApiTOrderMessageServiceImpl.class);

	/**
	 * 提醒接单
	 * 
	 * @param orderMessagePage
	 * @param map
	 * @throws Exception
	 */
	@Override
	@Transactional
	public void remindTakeOrder(OrderMessagePage orderMessagePage, Map<String, Object> map) throws Exception {
		ApiTOrderMessage orderMessage = null;

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderId", orderMessagePage.getOrderId());
		params.put("messageType", 2);// 2-提醒接单
		orderMessage = apiTOrderMessageMapper.selectTOrderMessage(params);
		if (orderMessage != null) {// 提醒过，更新最新的状态
			orderMessage.setIs_send(0);// 再次提醒--要判断是否在线--不在线不发，存储---再次登录时发送
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
		} else {// 没提醒过，插入数据
			orderMessage = new ApiTOrderMessage();
			orderMessage.setU_id(orderMessagePage.getU_id());
			orderMessage.setShop_id(orderMessagePage.getShop_id());
			orderMessage.setOrderId(orderMessagePage.getOrderId());
			orderMessage.setMessageType(2);// 2-提醒接单
			orderMessage.setIs_send(0);// 未发送
			orderMessage.setStatus(1);// 可读
			orderMessage.setInsertTime(new Date());
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.insertTOrderMessage(orderMessage);
			// 插入消息表数据
			ApiT_message apiT_message = new ApiT_message();
			apiT_message.setInserttime(orderMessage.getInsertTime());
			apiT_message.setMessagetype(orderMessage.getMessageType());
			apiT_message.setOperatetime(orderMessage.getOperateTime());
			apiT_message.setOrderid(orderMessage.getOrderId());
			apiT_message.setShop_id(orderMessage.getShop_id());
			apiT_message.setStatus(orderMessage.getStatus());
			apiT_message.setU_id(orderMessage.getU_id());
			apiT_message.setTitle("提醒接单");
			apiT_message.setIs_send(0);// 未读
			apiT_message.setContent("您好，客户提醒接单：" + orderMessagePage.getOrderId());
			apiT_messageMapper.insertT_message(apiT_message);
		}
		// 判断商家是否在线---根据shop_id 查询商铺信息，判断是否在线
		ApiShop shop = apiShopsMapper.findT_shopsById(orderMessagePage.getShop_id());
		Integer is_login = shop.getIs_login();
		if (1 == is_login) {
			orderMessage.setIs_send(1);// 已发送
			orderMessage.setStatus(1);// 有效的信息，可读
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
			/** 在线-给商家推送消息 每次提醒都给商家推送消息 */
			apiCommonService.remindShopToTakeOrder(orderMessagePage.getShop_id(), orderMessagePage.getOrderId());
		}
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "提醒成功！");

	}

	/**
	 * 提醒发货
	 * 
	 * @param orderMessagePage
	 * @param map
	 * @throws Exception
	 */
	@Override
	@Transactional
	public void remindSendGoods(OrderMessagePage orderMessagePage, Map<String, Object> map) throws Exception {
		ApiTOrderMessage orderMessage = null;

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderId", orderMessagePage.getOrderId());
		params.put("messageType", 3);
		orderMessage = apiTOrderMessageMapper.selectTOrderMessage(params);
		if (orderMessage != null) {// 提醒过，更新最新的状态
			orderMessage.setIs_send(0);// 未发送
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
		} else {// 没提醒过，插入数据
			orderMessage = new ApiTOrderMessage();
			orderMessage.setU_id(orderMessagePage.getU_id());
			orderMessage.setShop_id(orderMessagePage.getShop_id());
			orderMessage.setOrderId(orderMessagePage.getOrderId());
			orderMessage.setMessageType(3);// 提醒发货
			orderMessage.setIs_send(0);// 未发送
			orderMessage.setStatus(1);// 可读
			orderMessage.setInsertTime(new Date());
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.insertTOrderMessage(orderMessage);

			// 插入消息表数据
			ApiT_message apiT_message = new ApiT_message();
			apiT_message.setInserttime(orderMessage.getInsertTime());
			apiT_message.setMessagetype(orderMessage.getMessageType());
			apiT_message.setOperatetime(orderMessage.getOperateTime());
			apiT_message.setOrderid(orderMessage.getOrderId());
			apiT_message.setShop_id(orderMessage.getShop_id());
			apiT_message.setStatus(orderMessage.getStatus());
			apiT_message.setU_id(orderMessage.getU_id());
			apiT_message.setTitle("提醒发货");
			apiT_message.setIs_send(0);// 未读
			apiT_message.setContent("您好，客户提醒发货：" + orderMessagePage.getOrderId());
			apiT_messageMapper.insertT_message(apiT_message);
		}
		// 判断商家是否在线---根据shop_id 查询商铺信息，判断是否在线
		ApiShop shop = apiShopsMapper.findT_shopsById(orderMessagePage.getShop_id());
		Integer is_login = shop.getIs_login();
		if (1 == is_login) {
			orderMessage.setIs_send(1);// 已发送
			orderMessage.setStatus(1);// 有效的信息，可读
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
			/** 每次提醒都给商家推送消息 */
			apiCommonService.remindShopToSendGoods(orderMessagePage.getShop_id(), orderMessagePage.getOrderId());
		}

		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "提醒成功！");
	}

	@Override
	public void sendShopNewOrderMessage(ApiDingDan dingDan) {
		if (apiCommonService.checkNewOrder(dingDan.getZf_type(), dingDan.getDd_status(), dingDan.getZi_status())) {
			// 是新订单，需要给商家推送消息
			ApiTOrderMessage orderMessage = null;

			Map<String, Object> params = new HashMap<String, Object>();
			params.put("orderId", dingDan.getTitles());
			params.put("messageType", 1);// 商家新订单
			orderMessage = apiTOrderMessageMapper.selectTOrderMessage(params);
			if (orderMessage != null) {// 提醒过，更新最新的状态
				orderMessage.setIs_send(0);// 未发送
				orderMessage.setOperateTime(new Date());
				apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
			} else {// 没提醒过，插入数据
				orderMessage = new ApiTOrderMessage();
				orderMessage.setU_id(dingDan.getU_id());
				orderMessage.setShop_id(dingDan.getShop_id());
				orderMessage.setOrderId(dingDan.getTitles());
				orderMessage.setMessageType(1);// 商家新订单
				orderMessage.setIs_send(0);// 未发送
				orderMessage.setStatus(0);// 不可读
				orderMessage.setInsertTime(new Date());
				orderMessage.setOperateTime(new Date());
				apiTOrderMessageMapper.insertTOrderMessage(orderMessage);

				// 插入消息表数据
				ApiT_message apiT_message = new ApiT_message();
				apiT_message.setInserttime(orderMessage.getInsertTime());
				apiT_message.setMessagetype(orderMessage.getMessageType());
				apiT_message.setOperatetime(orderMessage.getOperateTime());
				apiT_message.setOrderid(orderMessage.getOrderId());
				apiT_message.setShop_id(orderMessage.getShop_id());
				apiT_message.setStatus(orderMessage.getStatus());
				apiT_message.setU_id(orderMessage.getU_id());
				apiT_message.setTitle("新订单");
				apiT_message.setIs_send(0);// 未读
				apiT_message.setContent("您好，有新的订单：" + dingDan.getTitles());
				apiT_messageMapper.insertT_message(apiT_message);
			}
			// 判断店铺是否在线-登录状态
			ApiShop shop = apiShopsMapper.findT_shopsById(dingDan.getShop_id());
			Integer is_login = shop.getIs_login();
			if (1 == is_login) {
				orderMessage.setIs_send(1);// 已发送
				orderMessage.setStatus(1);// 有效的信息，可读
				orderMessage.setOperateTime(new Date());
				apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
				apiCommonService.sendNewOrdersInfoToShop(dingDan.getShop_id(), dingDan.getTitles());
			}
		}
	}

	/**
	 * 提醒配送员有新的订单
	 */
	@Override
	public void remindSenderNewTask(String u_id, String titles) {
		// TODO Auto-generated method stub
		ApiTOrderMessage orderMessage = null;

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderId", titles);
		params.put("messageType", 4);// 配送员新订单
		orderMessage = apiTOrderMessageMapper.selectTOrderMessage(params);
		if (orderMessage != null) {// 提醒过，更新最新的状态
			orderMessage.setIs_send(0);// 未发送
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
		} else {// 没提醒过，插入数据
			orderMessage = new ApiTOrderMessage();
			orderMessage.setU_id(u_id);
			orderMessage.setOrderId(titles);
			orderMessage.setMessageType(4);// //配送员新订单
			orderMessage.setIs_send(0);// 未发送
			orderMessage.setStatus(0);// 不可读
			orderMessage.setInsertTime(new Date());
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.insertTOrderMessage(orderMessage);
			// 插入消息表数据
			ApiT_message apiT_message = new ApiT_message();
			apiT_message.setInserttime(orderMessage.getInsertTime());
			apiT_message.setMessagetype(orderMessage.getMessageType());
			apiT_message.setOperatetime(orderMessage.getOperateTime());
			apiT_message.setOrderid(orderMessage.getOrderId());
			apiT_message.setShop_id(orderMessage.getShop_id());
			apiT_message.setStatus(orderMessage.getStatus());
			apiT_message.setU_id(orderMessage.getU_id());
			apiT_message.setTitle("新的配送任务");
			apiT_message.setIs_send(0);// 未读
			apiT_message.setContent("您好，有新的配送任务：" + titles);
			apiT_messageMapper.insertT_message(apiT_message);
		}

		ApiTmember member = apiTmemberMapper.findMemberByCuserid(u_id);
		Integer is_login = member.getIs_login();
		if (1 == is_login) {
			orderMessage.setIs_send(1);// 已发送
			orderMessage.setStatus(1);// 有效的信息，可读
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
			apiCommonService.remindSenderNewTask(u_id, titles);
		}
	}

	/**
	 * 提醒客户订单在配送途中
	 */
	@Override
	public void remindUserOrderInSending(String u_id, String titles) {
		try {
			ApiTOrderMessage orderMessage = null;

			Map<String, Object> params = new HashMap<String, Object>();
			params.put("orderId", titles);
			params.put("messageType", 5);// 配送中
			orderMessage = apiTOrderMessageMapper.selectTOrderMessage(params);
			if (orderMessage != null) {// 提醒过，更新最新的状态
				orderMessage.setIs_send(0);// 未发送
				orderMessage.setOperateTime(new Date());
				apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
			} else {// 没提醒过，插入数据
				orderMessage = new ApiTOrderMessage();
				orderMessage.setU_id(u_id);
				orderMessage.setOrderId(titles);
				orderMessage.setMessageType(5);// 配送中
				orderMessage.setIs_send(0);// 未发送
				orderMessage.setStatus(0);// 不可读
				orderMessage.setInsertTime(new Date());
				orderMessage.setOperateTime(new Date());
				apiTOrderMessageMapper.insertTOrderMessage(orderMessage);
				// 插入消息表数据
				ApiT_message apiT_message = new ApiT_message();
				apiT_message.setInserttime(orderMessage.getInsertTime());
				apiT_message.setMessagetype(orderMessage.getMessageType());
				apiT_message.setOperatetime(orderMessage.getOperateTime());
				apiT_message.setOrderid(orderMessage.getOrderId());
				apiT_message.setShop_id(orderMessage.getShop_id());
				apiT_message.setStatus(orderMessage.getStatus());
				apiT_message.setU_id(orderMessage.getU_id());
				apiT_message.setTitle("订单配送中");
				apiT_message.setIs_send(0);// 未读
				apiT_message.setContent("您好，您的订单：" + titles + "已在配送途中，请时刻关注！");
				apiT_messageMapper.insertT_message(apiT_message);
			}

			ApiTmember member = apiTmemberMapper.findMemberByCuserid(u_id);
			Integer is_login = member.getIs_login();
			if (1 == is_login) {
				orderMessage.setIs_send(1);// 已发送
				orderMessage.setStatus(1);// 有效的信息，可读
				orderMessage.setOperateTime(new Date());
				apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
				apiCommonService.remindUserOrderInSending(u_id, titles);
			}
		} catch (Exception e) {
			logger.error(e);
		}

	}

	/**
	 * 给客户推送消息，让客户对已完成的订单进行评价
	 */
	@Override
	public void remindUserEvaluate(String u_id, String titles) {
		ApiTOrderMessage orderMessage = null;

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderId", titles);
		params.put("messageType", 6);// 已送达
		orderMessage = apiTOrderMessageMapper.selectTOrderMessage(params);
		if (orderMessage != null) {// 提醒过，更新最新的状态
			orderMessage.setIs_send(0);// 未发送
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
		} else {// 没提醒过，插入数据
			orderMessage = new ApiTOrderMessage();
			orderMessage.setU_id(u_id);
			orderMessage.setOrderId(titles);
			orderMessage.setMessageType(6);// 已送达
			orderMessage.setIs_send(0);// 未发送
			orderMessage.setStatus(0);// 不可读
			orderMessage.setInsertTime(new Date());
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.insertTOrderMessage(orderMessage);
			// 插入消息表数据
			ApiT_message apiT_message = new ApiT_message();
			apiT_message.setInserttime(orderMessage.getInsertTime());
			apiT_message.setMessagetype(orderMessage.getMessageType());
			apiT_message.setOperatetime(orderMessage.getOperateTime());
			apiT_message.setOrderid(orderMessage.getOrderId());
			apiT_message.setShop_id(orderMessage.getShop_id());
			apiT_message.setStatus(orderMessage.getStatus());
			apiT_message.setU_id(orderMessage.getU_id());
			apiT_message.setTitle("订单已送达");
			apiT_message.setIs_send(0);// 未读
			apiT_message.setContent("亲爱的客户您好，您的订单：" + titles + "已送达，请对本次配送服务评价！");
			apiT_messageMapper.insertT_message(apiT_message);
		}

		ApiTmember member = apiTmemberMapper.findMemberByCuserid(u_id);
		Integer is_login = member.getIs_login();
		if (1 == is_login) {
			orderMessage.setIs_send(1);// 已发送
			orderMessage.setStatus(1);// 有效的信息，可读
			orderMessage.setOperateTime(new Date());
			apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
			apiCommonService.remindUserEvaluate(u_id, titles);
		}
	}

	@Override
	public void remindSenderForClothes(String shop_id, String orderId) {
		// 根据订单号查询 订单状态、 配送员
		ApiDingDan dingDan = apiDingDanMapper.searchDingDanByTitleId(orderId);
		ApiT_shipping shipping = apiT_shippingMapper.findT_shippingByOrderId(orderId);
		ApiShop shop = apiShopsMapper.findShopByCid(shop_id);

		String u_id = shipping.getUser_id();
		Integer status = shipping.getStatus();
		if (!Util.isNull(u_id)) {
			// 查询配送员的登录设备
			ApiTOrderMessage orderMessage = null;

			Integer messageType = 8;// 配送员到商家取衣服
			String message = "您好，请到" + shop.getcAddress() + " " + shop.getcSname() + "取衣服,送到客户：" + dingDan.getName()
					+ ",地址：" + dingDan.getAddress();
			if (status == 2) {// 配送员接单，提醒配送员到客户处取衣服
				messageType = 7;
				message = "您好，请到客户：" + dingDan.getName() + ",地址：" + dingDan.getAddress() + "取衣服,送到" + shop.getcAddress()
						+ " " + shop.getcSname();
			}

			Map<String, Object> params = new HashMap<String, Object>();
			params.put("orderId", orderId);
			params.put("messageType", messageType);// 已送达
			orderMessage = apiTOrderMessageMapper.selectTOrderMessage(params);
			if (orderMessage != null) {// 提醒过，更新最新的状态
				orderMessage.setIs_send(0);// 未发送
				orderMessage.setOperateTime(new Date());
				apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
			} else {// 没提醒过，插入数据
				orderMessage = new ApiTOrderMessage();
				orderMessage.setU_id(u_id);
				orderMessage.setOrderId(orderId);
				orderMessage.setMessageType(messageType);// 已送达
				orderMessage.setIs_send(0);// 未发送
				orderMessage.setStatus(0);// 不可读
				orderMessage.setInsertTime(new Date());
				orderMessage.setOperateTime(new Date());
				apiTOrderMessageMapper.insertTOrderMessage(orderMessage);

				// 插入消息表数据
				ApiT_message apiT_message = new ApiT_message();
				apiT_message.setInserttime(orderMessage.getInsertTime());
				apiT_message.setMessagetype(orderMessage.getMessageType());
				apiT_message.setOperatetime(orderMessage.getOperateTime());
				apiT_message.setOrderid(orderMessage.getOrderId());
				apiT_message.setShop_id(orderMessage.getShop_id());
				apiT_message.setStatus(orderMessage.getStatus());
				apiT_message.setU_id(orderMessage.getU_id());
				apiT_message.setTitle("新的配送任务");
				apiT_message.setContent(message);
				apiT_message.setIs_send(0);// 未读
				apiT_messageMapper.insertT_message(apiT_message);
			}

			ApiTmember member = apiTmemberMapper.findMemberByCuserid(u_id);
			Integer is_login = member.getIs_login();
			if (1 == is_login) {
				orderMessage.setIs_send(1);// 已发送
				orderMessage.setStatus(1);// 有效的信息，可读
				orderMessage.setOperateTime(new Date());
				apiTOrderMessageMapper.updateTOrderMessage(orderMessage);
				apiCommonService.remindSenderForClothes(u_id, orderId, message, messageType);
			}
		}
	}

}
